<?php
/* @var $this TopicController */
/* @var $model Topic */
/* @var $form CActiveForm */
?>

<style type="text/css">
    #file{
        margin-left: 0px;
    }
    #imgUpload{
        display:none;
    }
</style>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'topic-form',
	'enableAjaxValidation'=>false,
        'htmlOptions'=>array(
            'enctype'=>'multipart/form-data'
        )
)); ?>
        <div id="imgUpload"></div>
	<div class="row">
		<?php echo $form->labelEx($model,'title'); ?>
		<?php echo $form->textField($model,'title'); ?>
		<?php echo $form->error($model,'title'); ?>
	</div>
    
        <div class="row">
            <?php echo $form->labelEx($model, 'imageId'); ?>
            <div id="file">
                <div id="thumb">
                    <?php if($model->avatar): ?>
                        <img src="<?php echo Yii::app()->baseUrl . '/' . $model->avatar->url ?>" width="100" height="100" />
                    <?php endif ?>
                </div>
                <?php
                Yii::app()->clientScript->registerCoreScript('jquery');
                Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl . '/js/plupload/plupload.full.js');
                ?>
                <div id="container">
                    <div id="filelist"></div>
                    <a id="pickfiles" href="#">[Select files]</a>
                    <a id="uploadfiles" href="#">[Upload files]</a>
                </div>
            </div>
        </div>

	<div class="row">
            <?php echo $form->labelEx($model, 'content'); ?>
            <?php
            $this->widget('ext.widgets.xheditor.XHeditor', array(
                'model' => $model,
                'modelAttribute' => 'content',
                'config' => array(
                    'id' => 'xheditor_1',
                    'tools' => 'mfull', // mini, simple, mfull, full or from XHeditor::$_tools, tool names are case sensitive
                    'skin' => 'default', // default, nostyle, o2007blue, o2007silver, vista
                    'width' => '640px',
                    'height' => '200px',
                    'loadCSS' => XHtml::cssUrl('editor.css'),
                    'upImgUrl' => $this->createUrl('request/uploadFile'),
                    'upImgExt' => 'jpg,jpeg,gif,png',
                ),
            ));
            ?>
        </div>

	<div class="row">
		<?php echo $form->labelEx($model,'categoryId'); ?>
		<?php echo $form->dropDownList($model,'categoryId', TopicCategory::model()->getIdNameArray(false)); ?>
		<?php echo $form->error($model,'categoryId'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'status'); ?>
		<?php echo $form->dropDownList($model,'status', array(1=>Yii::t('main', 'Visible'), 0=>Yii::t('main', 'Hidden'))); ?>
		<?php echo $form->error($model,'status'); ?>
	</div>
    
        <div class="row">
                <?php echo $form->labelEx($model,'upload'); ?>
                <?php 
                $attachment=$model->attachment;
                if($attachment): ?>
                <div style="display:block" id="attachlink">
                    <?php echo CHtml::link($attachment->name, Yii::app()->baseUrl . '/' . $attachment->url) ?>
                </div>
                <div style="display:block" id="attachaction">
                    <?php echo CHtml::button(Yii::t('main', 'Delete File'), array('id'=>'btndeletefile')) ?>
                </div>
                <script type="text/javascript">
                    $(function(){
                        $('#btndeletefile').click(function(){
                            if(confirm('<?php echo Yii::t('main', 'This action will delete file from system, are you sure?') ?>')){
                                $.ajax({
                                    url:'<?php echo Yii::app()->createUrl('admin/file/ajaxDelete', array('id'=>$attachment->id)) ?>',
                                    dataType:'json',
                                    success:function(data){
                                        if(data.success===1){
                                            $('#attachlink').remove();
                                            $('#attachaction').remove();
                                        }
                                    }
                                });
                            }
                        });
                    })
                </script>
                <?php endif ?>
                <?php echo $form->fileField($model,'upload'); ?>
                <?php echo $form->error($model,'upload'); ?>
        </div>

	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->

<script type="text/javascript">
    $(function() {
        var uploader = new plupload.Uploader({
            runtimes : 'gears,html5,flash,silverlight,browserplus',
            browse_button : 'pickfiles',
            container : 'container',
            max_file_size : '50mb',
            url : '<?php echo $this->createUrl('upload') ?>',
            flash_swf_url : '<?php echo Yii::app()->baseUrl ?>/js/plupload/plupload.flash.swf',
            silverlight_xap_url : '<?php echo Yii::app()->baseUrl ?>/js/plupload/plupload.silverlight.xap',
            filters : [
                {title : "Image files", extensions : "jpg,gif,png"},
                {title : "Zip files", extensions : "zip"}
            ],
            resize : {width : 320, height : 240, quality : 90}
        });
        $('#uploadfiles').click(function(e) {
            uploader.start();
            e.preventDefault();
        });

        uploader.init();

        uploader.bind('FilesAdded', function(up, files) {
            $.each(files, function(i, file) {
                $('#filelist').html(
                '<div id="' + file.id + '" class="imgItem">' +
                    file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
                    '</div>');
               
            });

            up.refresh(); // Reposition Flash/Silverlight
        });

        uploader.bind('UploadProgress', function(up, file) {  
            $('#' + file.id+" b").html(file.percent + "%");
    
        });

        uploader.bind('Error', function(up, err) {
            $('#filelist').append("<div>Error: " + err.code +
                ", Message: " + err.message +
                (err.file ? ", File: " + err.file.name : "") +
                "</div>"
        );

            up.refresh(); // Reposition Flash/Silverlight
        });

        uploader.bind('FileUploaded', function(up, file, response) {            
            var resObj=$.parseJSON(response.response);
            $('#' + file.id + " b").html("100%");
            $('#imgUpload').html('<input name="Topic[imageId]" value="'+resObj.id+'">');
            $('#file #thumb').html('<img src="'+resObj.url+'" width="100" height="100" />');           
        });
    });
</script>